﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace bvsoftware.bvtransfer
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            LoadInitialData();
        }

        public void LoadInitialData()
        {
            PopulateStoreList();
        }


        #region Store List Functions

        public void PopulateStoreList()
        {
            this.lstStores.DataSource = Core.StoreDataManager.FindAll();
            this.lstStores.DisplayMember = "FriendlyName";
            this.lstStores.ValueMember = "Id";
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.lstStores.ClearSelected();
        }

        private void lstStores_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.lstStores.SelectedItem == null)
            {
                this.FriendlyNameField.Text = string.Empty;
                this.SiteURLField.Text = string.Empty;
                this.UsernameField.Text = string.Empty;
                this.PasswordField.Text = string.Empty;
                this.button2.Text = "Add New Store";
            }
            else
            {
                Core.StoreData s = (Core.StoreData)this.lstStores.SelectedItem;
                this.FriendlyNameField.Text = s.FriendlyName;
                this.SiteURLField.Text = s.StoreUrl;
                this.UsernameField.Text = s.Username;
                this.PasswordField.Text = s.Password;
                this.button2.Text = "Update Store";
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            string modId = string.Empty;

            if (this.lstStores.SelectedItem != null)
            {
                // update
                Core.StoreData s = (Core.StoreData)this.lstStores.SelectedItem;
                if (s != null)
                {
                    modId = s.Id;
                    s.FriendlyName = this.FriendlyNameField.Text.Trim();
                    s.StoreUrl = this.SiteURLField.Text.Trim();
                    s.Username = this.UsernameField.Text.Trim();
                    s.Password = this.PasswordField.Text.Trim();
                }
                List<Core.StoreData> all = (List<Core.StoreData>)this.lstStores.DataSource;
                if (all != null)
                {
                    Core.StoreDataManager.SaveAll(all);
                }
            }
            else
            {
                // add new
                Core.StoreData s = new Core.StoreData();
                s.Id = System.Guid.NewGuid().ToString();
                s.FriendlyName = this.FriendlyNameField.Text.Trim();
                s.StoreUrl = this.SiteURLField.Text.Trim();
                s.Username = this.UsernameField.Text.Trim();
                s.Password = this.PasswordField.Text.Trim();
                modId = s.Id;
                List<Core.StoreData> all = (List<Core.StoreData>)this.lstStores.DataSource;
                if (all != null)
                {
                    all.Add(s);
                    Core.StoreDataManager.SaveAll(all);
                }
            }
            this.lstStores.ClearSelected();
            PopulateStoreList();
            if (modId != string.Empty)
            {
                foreach (Core.StoreData sd in this.lstStores.Items)
                {
                    if (sd.Id == modId)
                    {
                        this.lstStores.SelectedItem = sd;
                        break;
                    }
                }                
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            List<Core.StoreData> all = (List<Core.StoreData>)this.lstStores.DataSource;
            if (all != null)
            {
                all.Remove((Core.StoreData)this.lstStores.SelectedItem);
                Core.StoreDataManager.SaveAll(all);
            }
            this.lstStores.ClearSelected();
            PopulateStoreList();
        }

        #endregion


    }
}
